<?php


namespace App\Models;


use App\User;
use Illuminate\Support\Facades\DB;

class UserDeal extends BaseModel
{
    protected $table = 'user_deal';

    public static function selectRaw()
    {
        $select = 'id,user_id,future_vip_deal,vip_deal,deposit_vip_del,full_5000_num,deal_time,future_vip_deal+vip_deal+deposit_vip_del as today_sum_yeji';
        return DB::raw($select);
    }

    public static function getDealByWhere($where)
    {
        $model = UserDeal::where($where)
            ->select(self::selectRaw())
            ->first();
        return $model;
    }

    public static function getDealByDateWhere($where,$date)
    {
        $model = UserDeal::where($where)
            ->whereBetween('deal_time', $date)
            ->select(self::selectRaw())
            ->first();
        return $model;
    }

    //周环比计算
    public static function weekRate($user_id, $date)
    {
        $where = [
            ['user_id', '=', $user_id],
        ];
        $now_week_model =  UserDeal::getDealByDateWhere($where, uweeek($date));
        $last_week_model = UserDeal::getDealByDateWhere($where, ulast_weeek($date));
        if($now_week_model && $last_week_model){
            return BaseModel::ringRate($now_week_model['today_sum_yeji'],$last_week_model['today_sum_yeji']);
        }
        return 0;
    }

    //月环比计算
    public static function monthRate($user_id, $date)
    {
        $where = [
            ['user_id', '=', $user_id],
        ];
        $now_month_model = UserDeal::getDealByDateWhere($where, umonth($date));
        $last_week_model = UserDeal::getDealByDateWhere($where, ulast_month($date));
        if($now_month_model && $last_week_model){
            return  BaseModel::ringRate($now_month_model['today_sum_yeji'],$last_week_model['today_sum_yeji']);
        }
        return 0;
    }

    public static function dayList($user_id, $date, $type = 1)
    {
        $now_days = [];
        if($type == 1){
            $now_days[] = $date;
        }
        if($type == 2){
            $now_days = get_week_days($date);
        }
        if($type == 3){
            $now_days = get_month_days($date);
        }
        $sum_array = [];
        $tongbi_array = [];
        foreach($now_days as $key => $val){
            $now_where = [
                ['user_id', '=', $user_id],
                ['deal_time', '=', $val],
            ];
            $now_model = self::getDealByWhere($now_where);
            $last = date("Y-m-d", strtotime("-1 years",strtotime($val)));
            $last_where = [
                ['user_id', '=', $user_id],
                ['deal_time', '=', $last],
            ];
            $last_model = self::getDealByWhere($last_where);
            $tongbi = 0;
            if($last_model && $now_model){
                $tongbi = self::ringRate($last_model['today_sum_yeji'],$now_model['today_sum_yeji']);
            }
            $sum_array[] = !empty($now_model['today_sum_yeji'])?$now_model['today_sum_yeji']:0;
            $tongbi_array[] = $tongbi;
        }
        $new_data = self::twoLine($now_days, $tongbi_array, $sum_array);
        return $new_data;
    }

    public static function yejiliangRate($deal)
    {
        $zhun_vip = 0;
        $vip = 0;
        $zhunzhi_vip = 0;
        if($deal){
            $sum = $deal['future_vip_deal']+$deal['vip_deal']+$deal['deposit_vip_del'];
            $zhun_vip = self::roundRingRate($deal['future_vip_deal'],$sum);
            $vip = self::roundRingRate($deal['vip_deal'],$sum);
            $zhunzhi_vip = self::roundRingRate($deal['deposit_vip_del'],$sum);
        }
        return [$zhun_vip, $vip, $zhunzhi_vip];
    }

    public static function getRank($store_id)
    {
        //$store_id = 11;
        $userModel = User::getStoreUserId($store_id);
        $ids = [];
        if($userModel){
            $ids = array_column($userModel, 'id');
        }
        $field = 'id,user_id,sum(full_5000_num) as sum_full_5000_num,sum(future_vip_deal) as sum_future_vip_deal,sum(vip_deal) as sum_vip_deal,sum(deposit_vip_del) as sum_deposit_vip_del';
        $list = self::select(DB::raw($field))
            ->whereIn('user_id', $ids)
            ->groupBy('user_id')
            ->get()
            ->toArray();
        foreach($list as $key=>&$val){
            $val['sum_money'] = $val['sum_future_vip_deal'] + $val['sum_vip_deal'] + $val['sum_deposit_vip_del'];
            $val['full_5000_num'] = $val['sum_full_5000_num'];
        }
        array_multisort(array_column($list,'sum_money'),SORT_DESC,$list);
        $ranking = 1;
        foreach($list as $key =>&$val){
            $user = User::getSimpleUserById($val['user_id'],true);
            if($user){
                $val['user'] = $user;
            }
            $val['ranking'] = $key+$ranking;
        }
        if(empty($list)){
            $list = $userModel;
            $ranking = 1;
            foreach ($list as $key=>&$val) {
                $user = User::getSimpleUserById($val['id'],true);
                if($user){
                    $val['user'] = $user;
                }
                $val['full_5000_num'] = 0;
                $val['sum_money'] = 0;
                $val['ranking'] = $key + $ranking;
            }
        }
        return $list;
    }

    public static function createRecord($input)
    {
        $where = [
            ['user_id', '=', $input['user_id']],
            ['deal_time', '=', $input['deal_time']],
        ];
        $user = User::find($input['user_id']);
        if (!$user) {
            uexception('店员不存在');
        }
        $model = self::where($where)->first();
        if ($model) {
            uexception('该日期已添加');
        }
        $model = new self();
        $model = self::createModel($model, $input);
        return $model;
    }
}